<template>
  <h1> 我是产品列表</h1>
  <el-table :data="productList" style="width: 99%">
    <el-table-column label="产品id" prop="id"/>
    <el-table-column label="产品标题" prop="name"/>
    <el-table-column label="产品价格" prop="price"/>
    <el-table-column label="产品标签" prop="tags"/>
    <el-table-column label="创建时间" prop="createTime"/>
    <el-table-column align="right">
      <template #header>
        <el-input v-model.trim="keyword" size="small" placeholder="请输入产品名称"/>
      </template>
      <template #default>
        <el-button size="small">
          编辑
        </el-button>
        <el-button
            size="small"
            type="danger"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>

import productService from "@/services/ProductService";

export default {
  name: "index",
  data() {
    return {
      productList: [],
      keyword: "",
      total: 0,
      pageNo: 1,
      pageSize: 10,
      pages: 0
    }
  },
  created() {
    // 初始化调用，异步查询产品有关信息
    this.loadProduct();
  },
  methods: {
    // 查询产品列表并分页
    async loadProduct() {
      try {
        let res = await productService.loadProduct();
        if (res.data.status == 200) {
          let {total, size, records, current, pages} = res.data.data;
          this.productList = records;
          this.total = total;
          this.pageNo = current;
          this.pageSize = size;
          this.pages = pages;
        }
      } catch (err) {
        console.log(err);
      }
    },

    // 根据产品id查询产品信息
    async getProductById(index) {
      try {
        const pid = this.productList[index].id;
        let res = await productService.getProductById(pid);
        console.log(res);
      } catch (err) {
        console.log(err);
      }
    }
  }
}

</script>

<style scoped>

</style>